package com.automannn.websocketmqtt.rocketmq.nodeA.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenkh
 * @time 2020/11/2 10:26
 */
@RestController
@RequestMapping("/mqProducer")
public class MQProducerController {
    public static final Logger LOGGER = LoggerFactory.getLogger(MQProducerController.class);

    @Autowired
    DefaultMQProducer defaultMQProducer;

    @GetMapping("send")
    public String send(String msg) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        if (StringUtils.isEmpty(msg)){
            return "失败，参数为空!";
        }
        LOGGER.info("发送MQ消息内容:"+msg);
        Message sendMsg = new Message("TestTopic","TestTag",msg.getBytes());

        SendResult sendResult = defaultMQProducer.send(sendMsg);
        LOGGER.info("消息发送响应:"+sendResult.toString());
        return "成功!";
    }
}
